﻿using System;
using Spring.Context.Support;
using Common.Logging;
using System.IO;
using System.Collections;
using Spring.Scheduling.Quartz;

namespace DotNetServiceLib
{
    public class ApplicationContext
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(ApplicationContext));
        private string contextFileName = "";
        public static string FileName = "spring-objects.xml";

        public ApplicationContext(string contextFileName) {
            this.contextFileName = contextFileName;
        }

        public bool Load(ref string returnMessage)
        {
            bool isSuccess = false;
            if (File.Exists(this.contextFileName))
            {
                try
                {
                    XmlApplicationContext ctx = new XmlApplicationContext(this.contextFileName);
                    returnMessage += "Spring configuration succeeded, quartz jobs running.\n\n";
                    IDictionary dict = ctx.GetObjectsOfType(typeof(JobDetailObject));
                    foreach (DictionaryEntry kvp in dict)
                    {
                        returnMessage += "- " + ((JobDetailObject)kvp.Value).Description + "\n";
                    }
                }
                catch (Exception ex)
                {
                    returnMessage += String.Format("An error occured: {0}.", ex);
                }
            }
            else
            {
                returnMessage += String.Format("File could not be found in: {0}.", this.contextFileName);
            }
            return isSuccess;
        }
    }
}
